#include "stdafx.h"
#include "CPaintHDC.h"


CPaintHDC::CPaintHDC(HDC hdc)
	:m_hdc(hdc), m_hPen(NULL), m_hBrush(NULL)
{
}
CPaintHDC::~CPaintHDC()
{

}

void CPaintHDC::SetPixel(int_32 iX, int_32 iY, uint_32 uiColor)
{
	if(!m_hdc)
		return ;
	::SetPixel(m_hdc, iX, iY, uiColor);
}
uint_32 CPaintHDC::GetPixel(int_32 iX, int_32 iY)
{
	if(!m_hdc)
		return 0xFF000000;
	return ::GetPixel(m_hdc, iX, iY) | 0xFF000000;
}
void CPaintHDC::DrawLine(int_32 iX1, int_32 iY1, int_32 iX2, int_32 iY2,
	float_32 fLineWidth, uint_32 uiLineColor)
{
	if(!m_hdc)
		return ;
	SetSolidPen(fLineWidth, uiLineColor);
	SetNullBrush();

	::MoveToEx(m_hdc, iX1, iY1, NULL);
	::LineTo(m_hdc, iX2, iY2);
}
void CPaintHDC::DrawRect(int_32 iX1, int_32 iY1, int_32 iWidth, int_32 iHeight, 
	float_32 fLineWidth, uint_32 uiLineColor)
{
	if(!m_hdc)
		return ;

	SetSolidPen(fLineWidth, uiLineColor);
	SetNullBrush();
	::Rectangle(m_hdc, iX1, iY1, iX1 + iWidth, iY1 + iHeight);
}

void CPaintHDC::FillRect(int_32 iX1, int_32 iY1, int_32 iWidth, int_32 iHeight, uint_32 uiBrushColor)
{
	if(!m_hdc)
		return ;

	SetNullPen();
	SetSolidBrush(uiBrushColor);
	::Rectangle(m_hdc, iX1, iY1, iX1 + iWidth, iY1 + iHeight);
}

void CPaintHDC::DrawText(const char_16 * szText, int_32 iX, int_32 iY, uint_32 uiTextColor)
{
	::SetBkMode(m_hdc, TRANSPARENT);
	::SetTextColor(m_hdc, uiTextColor);
	TextOutW(m_hdc, iX, iY, szText, wcslen(szText));
}

void CPaintHDC::SetNullBrush()
{
	DeleteObject(SelectObject(m_hdc, GetStockObject(NULL_BRUSH)));
}
void CPaintHDC::SetSolidBrush(uint_32 uiColor)
{
	DeleteObject(SelectObject(m_hdc, CreateSolidBrush(uiColor)));
}
void CPaintHDC::SetBrush(HBRUSH hBrush)
{
	DeleteObject(SelectObject(m_hdc, hBrush));
}
void CPaintHDC::SetNullPen()
{
	DeleteObject(SelectObject(m_hdc, GetStockObject(NULL_PEN)));
}
void CPaintHDC::SetSolidPen(float_32 fWidth, uint_32 uiColor)
{
	DeleteObject(SelectObject(m_hdc, CreatePen(PS_SOLID, (int)fWidth, uiColor)));
}
void CPaintHDC::SetPen(HPEN hPen)
{
	DeleteObject(SelectObject(m_hdc, hPen));
}
void CPaintHDC::SetFont(const char_16 * szFace, uint_32 uiSize)
{
	HFONT hFont = CreateFontW(uiSize, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0, DEFAULT_CHARSET, 
		OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_NATURAL_QUALITY, DEFAULT_PITCH | FF_SWISS,
		szFace);
	DeleteObject(SelectObject(m_hdc, hFont));
}